package loge

import (
	"fmt"
	"os"

	"log"
	"time"
	"net/http"
)

var IsDebug = false
var logPath string

func getLogFileName() string {
	return time.Now().Format("/log_2006-01-02") + ".log"
}

func SetLogPath(path string) {
	logPath = path
	name := getLogFileName()
	os.MkdirAll(path, os.ModePerm)
	f, err := os.OpenFile(path+name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalln("%T err:%s", err, err)
	}
	log.SetOutput(f)
}
func SetHttp(mux *http.ServeMux) {
	mux.Handle("/log/", http.StripPrefix("/log/", http.FileServer(http.Dir(logPath))))
}
func WD(a ...interface{}) {
	if !IsDebug {
		return
	}
	W(a)
}
func W(a ...interface{}) {
	log.Println(a)
	fmt.Fprintln(os.Stdout, a...)
}
func WDf(format string, a ...interface{}) {
	log.Printf(format, a...)
	fmt.Fprintf(os.Stdout, format, a...)
}
func ENullable(a interface{}) {
	if a != nil {
		WD(a)
	}
}
func Panic(a interface{}) {
	W(a)
	panic(a)
}
func PanicNullable(a interface{}) {
	if a != nil {
		Panic(a)
	}

}
